import wx
try:
    from agw import ultimatelistctrl as ULC
except ImportError: # if it's not there locally, try the wxPython lib.
    from wx.lib.agw import ultimatelistctrl as ULC
from wx.lib.newevent import NewEvent
(StartRequest, EVT_START_REQUEST) = NewEvent()


# Main source path for files.
app_path = "D:\\Eigene Datein\\Python\\TIGS DB\\"
source_path = app_path + "files\\"

class SequenceListCtrl(ULC.UltimateListCtrl):

    def __init__(self, parent, data):
        """Listctrl panel for the Sequence data."""
        ULC.UltimateListCtrl.__init__(self, parent, -1,
                                      agwStyle=wx.LC_REPORT|wx.LC_VIRTUAL|
                                      wx.LC_HRULES|wx.LC_VRULES|ULC.ULC_SHOW_TOOLTIPS)


        # Create the columns.
        lables = ["pIPKTA ID", "Tube ID", "Corrected ID", "GABI Phenom ID",
                  "Construct function", "Vector", "Plate", "Position",
                  "Longest Sequence", "Primer Left", "Primer Right", "RNAi Sequence"]
        row = 0
        for lable in lables:
             self.InsertColumn(row, lable)
             self.SetColumnWidth(row, 120)
             row += 1

        self.selected = ''

        # Look and feel
        self.SetItemCount(1000)
        self.attr1 = ULC.UltimateListItemAttr()
        self.attr1.SetBackgroundColour(wx.NamedColour("yellow"))
        self.attr2 = ULC.UltimateListItemAttr()
        self.attr2.SetBackgroundColour(wx.NamedColour("light blue"))

        # Get the requested data.
        if data != None:
            self.itemDataMap = data
        else:
            data = {}
        self.itemIndexMap = data.keys()
        self.SetItemCount(len(data))

        self.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnRightClick)
        self.Bind(wx.EVT_RIGHT_UP, self.OnRightClick)
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)

    def OnItemSelected(self, event):
        self.currentItem = event.m_itemIndex
        self.selected = self.GetItemText(self.currentItem)


    def getColumnText(self, index, col):
        item = self.GetItem(index, col)
        return item.GetText()

    #---------------------------------------------------
    # Insert data.
    def OnGetItemText(self, item, col):
        index=self.itemIndexMap[item]
        s = self.itemDataMap[index][col]
        return s

    def OnGetItemToolTip(self, item, col):
        return None

    def OnGetItemTextColour(self, item, col):
        return None

    def OnGetItemAttr(self, item):
        return None
#        if item % 3 == 1:
#            return self.attr1
#        else:
#            return self.attr2

    def OnRightClick(self, event):

        if not hasattr(self, "popupID1"):
            self.popupID1 = wx.NewId()
            self.Bind(wx.EVT_MENU, self.OnPopupOne, id=self.popupID1)

        # make a menu
        menu = wx.Menu()
        # add some items
        menu.Append(self.popupID1, "Start TIGS Request")
        self.PopupMenu(menu)
        menu.Destroy()

    def OnPopupOne(self, event):
        """Popup menu to start TIGS request of hit ID."""
        f_hit = open(source_path + 'hit_request.txt', 'w')
        f_hit.write(str(self.selected))
        f_hit.close()
        # Event-Instanz erstellen
        evt = StartRequest()
        # Event starten
        self.ProcessEvent(evt)



#
#    def OnGetItemColumnCheck(self, item, column):
#
#        if item%3 == 0:
#            return True
#
#        return False
#
#
#    def OnGetItemCheck(self, item):
#
#        if item%3 == 1:
#            return True
#
#        return False
#
#
#    def OnGetItemColumnKind(self, item, column):
#
#        if item%3 == 0:
#            return 2
#        elif item%3 == 1:
#            return 1
#
#        return 0
